package com.jingan.nursinghome.regulatory.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import cn.android.support.v7.lib.sin.crown.base.BaseView;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 整改
 * Created by 彭治铭 on 2017/10/13.
 */

public class RectificationView extends BaseView {
    public RectificationView(Context context) {
        super(context);
        init();
    }

    public RectificationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    int textSize;

    private void init() {
        textSize = ProportionUtils.getInstance().adapterInt(32);
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.LEFT);
        textPaint.setColor(Color.WHITE);
    }

    int widht = 0;
    int height = 0;

    public void setWidhtAndHeiht(int widht, int height) {
        this.widht = widht;
        this.height = height;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (widht != 0 && height != 0) {
            setMeasuredDimension(widht, height);
        } else {
            setMeasuredDimension(ProportionUtils.getInstance().adapterInt(193), ProportionUtils.getInstance().adapterInt(91));
        }
    }

    String info;
    int bgColor;

    public void setInfoAndColor(String info, int bgColor) {
        this.info = info;
        this.bgColor = bgColor;
        invalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (info == null) {
            return;
        }
        textPaint.setTextSize(textSize);
        paint.setColor(bgColor);
        if (getWidth() < ProportionUtils.getInstance().adapterInt(190)) {
            canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), getWidth() / 3, getWidth() / 3, paint);
        } else {
            canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), getWidth() / 4, getWidth() / 4, paint);
        }
        int infoWidth = (int) textPaint.measureText(info, 0, info.length());
        //canvas.drawText(info, (getWidth() - infoWidth) / 2, (float) (textSize * 1.7), textPaint);
        if (info.length() >= 4 && getWidth() < ProportionUtils.getInstance().adapterInt(190)) {
            textPaint.setTextSize(textSize * 0.7f);
        }
        textPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(info, (getWidth()) / 2, (getHeight() - textSize * 0.8f) / 2 + textSize * 0.8f, textPaint);
    }
}
